package com.mycase.im.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mycase.im.boot.entity.database.ImMessage;
import com.mycase.im.boot.entity.database.ImUserFriend;
import com.mycase.im.boot.exception.CustomException;
import com.mycase.im.boot.mapper.MessageMapper;
import com.mycase.im.boot.mapper.UserFriendMapper;
import com.mycase.im.boot.mapper.UserMapper;
import com.mycase.im.boot.service.UserFriendService;
import com.mycase.im.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Author ZhuHJay
 * @Date 2021/8/13 19:09
 */
@Service
@Transactional(rollbackFor = CustomException.class)
public class UserFriendServiceImpl extends ServiceImpl<UserFriendMapper, ImUserFriend> implements UserFriendService {

    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<ImUserFriend> getFriendList(String id) {
        QueryWrapper<ImUserFriend> imUserFriendQueryWrapper = new QueryWrapper<>();
        imUserFriendQueryWrapper.eq("user_id", id);
        // 获取到该用户的好友列表
        return list(imUserFriendQueryWrapper);
    }

    @Override
    public Boolean isFriendRelation(String reqId, String respId) {
        QueryWrapper<ImUserFriend> imUserFriendQueryWrapper = new QueryWrapper<>();
        imUserFriendQueryWrapper.and(wrapper ->
                wrapper.eq("user_id", reqId)
                        .eq("friend_id", respId));
        return getOne(imUserFriendQueryWrapper) != null;
    }

    @Override
    public ImUserFriend getFriendInfoByReplyApply(String respId, String reqId) {
        // 判断两个人是否是好友关系
        QueryWrapper<ImUserFriend> imUserFriendQueryWrapper = new QueryWrapper<>();
        imUserFriendQueryWrapper
                .eq("user_id", respId)
                .eq("friend_id", reqId);
        ImUserFriend imUserFriend = getOne(imUserFriendQueryWrapper);
        if( imUserFriend == null ){
            throw new CustomException("不是好友关系");
        }
        // 发送一条消息给申请人
        ImMessage imMessage = new ImMessage();
        imMessage.setSendTime(new Date());
        // 发送人 -> 回复申请的人
        imMessage.setFromId(respId);
        // 接收人 -> 申请的人
        imMessage.setToId(reqId);
        // 发送消息内容 -> 告知申请通过
        imMessage.setContent("已经成为好友了, 快来聊天吧!");
        // 系统通知类型
        imMessage.setNotify(1);
        // 单聊
        imMessage.setType(0);
        // 消息未读
        imMessage.setReadStatus(0);
        // 持久化该聊天信息到数据库
        messageMapper.insert(imMessage);
        // 将该聊天记录存到bean中
        imUserFriend.setChatRecord(Collections.singletonList(imMessage));
        // 设置未读消息条数
        imUserFriend.setUnReadCount(0);
        // 获取好友的信息
        imUserFriend.setImUser(userMapper.findById(reqId));
        return imUserFriend;
    }

    @Override
    public ImUserFriend getFriendInfoByApply(String reqId, String respId) {
        // 判断两个人是否是好友关系
        QueryWrapper<ImUserFriend> imUserFriendQueryWrapper = new QueryWrapper<>();
        imUserFriendQueryWrapper
                .eq("user_id", reqId)
                .eq("friend_id", respId);
        ImUserFriend imUserFriend = getOne(imUserFriendQueryWrapper);
        if( imUserFriend == null ){
            throw new CustomException("不是好友关系");
        }
        // 获取好友的信息
        imUserFriend.setImUser(userMapper.findById(respId));
        return imUserFriend;
    }

    @Override
    public void setFriendRemark(String userId, String friendId, String remark) {
        UpdateWrapper<ImUserFriend> imUserFriendUpdateWrapper = new UpdateWrapper<>();
        imUserFriendUpdateWrapper
                .set("remark", remark)
                .eq("user_id", userId)
                .eq("friend_id", friendId);
        if( !update(imUserFriendUpdateWrapper) ){
            throw new CustomException("修改失败, 请重试");
        }
    }

    @Override
    public void cutFriendRelation(String userId, String friendId) {
        try {
            QueryWrapper<ImUserFriend> imUserFriendQueryWrapper = new QueryWrapper<>();
            imUserFriendQueryWrapper
                    .eq("user_id", userId)
                    .eq("friend_id", friendId);
            baseMapper.delete(imUserFriendQueryWrapper);
            imUserFriendQueryWrapper.clear();
            imUserFriendQueryWrapper
                    .eq("user_id", friendId)
                    .eq("friend_id", userId);
            baseMapper.delete(imUserFriendQueryWrapper);
        }catch (Exception e){
            throw new CustomException("好友关系解除失败, 请重试");
        }
    }

}
